//
//  main.swift
//  swift01
//
//  Created by 张文升 on 2017/9/11.
//  Copyright © 2017年 张文升. All rights reserved.
//

import Foundation


struct Student{
    var name:String
    var mathScore:Int
    var englishScore:Int
    var average:Float
    
    //MARK: - 初始化并求平均值
    init(name:String,mathScore:Int,englishScore:Int) {
        self.name = name
        self.mathScore = mathScore
        self.englishScore = englishScore
        self.average = Float(self.mathScore + self.englishScore) / 2
    }
}
func sortWithAverage(students:[Student]) -> [Student] {
    var result = students
    
    for i in 0...9 {
        for j in i...9 {
            if result[i].average < result[j].average{
                let tmp = result[i]
                result[i] = result[j]
                result[j] = tmp
            }
        }
    }
    
    return result
}



//构造10个学生对象
var student01 = Student(name: "小红", mathScore: 44, englishScore: 58)
var student02 = Student(name: "小明", mathScore: 57, englishScore: 56)
var student03 = Student(name: "小刚", mathScore: 47, englishScore: 76)
var student04 = Student(name: "小李", mathScore: 55, englishScore: 84)
var student05 = Student(name: "二弟", mathScore: 85, englishScore: 95)
var student06 = Student(name: "小王", mathScore: 87, englishScore: 94)
var student07 = Student(name: "晓明", mathScore: 82, englishScore: 74)
var student08 = Student(name: "小张", mathScore: 93, englishScore: 66)
var student09 = Student(name: "小马", mathScore: 66, englishScore: 53)
var student10 = Student(name: "二弟弟", mathScore: 59, englishScore: 82)



//调用排序方法
var result = sortWithAverage(students: [student01,
                                        student02,
                                        student03,
                                        student04,
                                        student05,
                                        student06,
                                        student07,
                                        student08,
                                        student09,
                                        student10])

print("英语和数学平均分的成绩从小到大排列为:")
//遍历结果数组并打印
for i in 0..<result.count{
    print("姓名:\(result[i].name),数学成绩:\(result[i].mathScore),英语成绩:\(result[i].englishScore),平均分:\(result[i].average)")
}
